package org.tale.mscp.notify.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.tale.mscp.notify.entity.AnnualLeave;
import org.tale.mscp.notify.entity.CommonJsonFormat;
import org.tale.mscp.notify.services.ILeaveQueryService;
import org.tale.mscp.notify.utils.GsonUtils;
import org.tale.mscp.notify.utils.ParameterException;

import java.util.List;

/**
 * @program: mscp
 * @author: tale-mi
 * @description:
 * @create: 2019/6/15 17:21
 **/
@RestController
@RequestMapping({"/leavequery"})
@CrossOrigin(origins = "*")
public class LeaveQueryController {
    @Autowired
    ILeaveQueryService leaveQueryService;

    public LeaveQueryController() {
    }

    @RequestMapping(
            value = {"/nxj"},
            method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS, RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.PUT}
    )
    public String getPersonApi(String name, String year) {
        CommonJsonFormat commonJsonFormat = new CommonJsonFormat();

        try {
            List<AnnualLeave> list = this.leaveQueryService.getAvailableAnnualLeave(name, year);
            if (list.size() == 1) {
                commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.success);
                commonJsonFormat.setResultDesc("正确返回");
                commonJsonFormat.setJsonContent(list.get(0));
            } else {
                commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.fail);
                commonJsonFormat.setResultDesc("存在重复姓名，无法获取年休天数！");
            }
        } catch (ParameterException var8) {
            commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.fail);
            commonJsonFormat.setResultDesc("参数错误");
        } finally {
            return GsonUtils.gsonLocalDate.toJson(commonJsonFormat);
        }
    }

    @RequestMapping(
            value = {"/nxjjsonp"},
            method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.OPTIONS, RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.PUT}
    )
    public String getNxjApiJsonp(String name, String year) {
        CommonJsonFormat commonJsonFormat = new CommonJsonFormat();

        try {
            List<AnnualLeave> list = this.leaveQueryService.getAvailableAnnualLeave(name, year);
            if (list.size() == 1) {
                commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.success);
                commonJsonFormat.setResultDesc("正确返回");
                commonJsonFormat.setJsonContent(list.get(0));
            } else {
                commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.fail);
                commonJsonFormat.setResultDesc("存在重复姓名，无法获取年休天数！");
            }
        } catch (ParameterException var8) {
            commonJsonFormat.setResult(CommonJsonFormat.ResultEnum.fail);
            commonJsonFormat.setResultDesc("参数错误");
        } finally {
            return "callback(" + GsonUtils.gsonLocalDate.toJson(commonJsonFormat) + ")";
        }
    }

}
